﻿using Bandit.Helpers;
using ETC.DLEDLightingSystemPlus.Models;
using NPOI.HSSF.Record.PivotTable;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Windows;
using System.Windows.Documents;
using static NPOI.HSSF.Util.HSSFColor;

namespace ETC.DLEDLightingSystemPlus.Core
{
    public class TestConfig : Bandit.UI.MVVM.NotifyObject
    {
        private string _TestConfigFileName = "".GetBaseDirectory() + "TestConfig.xml";

        /// <summary>
        /// 当前加载的配置的文件名
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public string FileName { get; set; }

        private string _Project;
        /// <summary>
        /// 专案名
        /// </summary>
        public string Project
        {
            get { return _Project; }
            set
            {
                _Project = value;
                OnPropertyChanged("Project");
            }
        }

        private string _Station;
        /// <summary>
        /// 站点名
        /// </summary>
        public string Station
        {
            get { return _Station; }
            set
            {
                _Station = value;
                OnPropertyChanged("Station");
            }
        }

       

        private int _TestCaseType=2;
        public int TestCaseType
        {
            get { return _TestCaseType; }
            set { _TestCaseType = value; OnPropertyChanged(nameof(TestCaseType)); }
        }

        #region 漏电流补偿
        private double _VLEDFullyClosedCurrentOffset = 0;
        /// <summary>
        /// VLEDFullyClosedCurrentOffset 补偿值
        /// </summary>
        public double VLEDFullyClosedCurrentOffset
        {
            get { return _VLEDFullyClosedCurrentOffset; }
            set
            {
                _VLEDFullyClosedCurrentOffset = value;
                OnPropertyChanged("VLEDFullyClosedCurrentOffset");
            }
        }
        #endregion

        #region visility Set
        private bool _IsVisilitySetPattern = true;
        public bool IsVisilitySetPattern
        {
            get { return _IsVisilitySetPattern; }
            set
            {
                _IsVisilitySetPattern = value;

                OnPropertyChanged("IsVisilitySetPattern");

                IsVisilitySetPattern11 = !_IsVisilitySetPattern;
            }
        }
        private bool _IsVisilitySetPattern11 = false;
        public bool IsVisilitySetPattern11
        {
            get { return _IsVisilitySetPattern11; }
            set
            {
                _IsVisilitySetPattern11 = value;
                OnPropertyChanged("IsVisilitySetPattern11");
            }
        }
        #endregion

        #region Case 11 CheckerBoard

        private int _CheckerBoardSetMC = 1;
        public int CheckerBoardSetMC
        {
            get { return _CheckerBoardSetMC; }
            set
            {
                _CheckerBoardSetMC = value;
                OnPropertyChanged(nameof(CheckerBoardSetMC));
            }
        }

        private int _CheckerBoardIC1 = 0;
        public int CheckerBoardIC1
        {
            get { return _CheckerBoardIC1; }
            set
            {
                _CheckerBoardIC1 = value;
                OnPropertyChanged("CheckerBoardIC1");
            }
        }

        private int _CheckerBoardIC2 = 0;
        public int CheckerBoardIC2
        {
            get { return _CheckerBoardIC2; }
            set
            {
                _CheckerBoardIC2 = value;
                OnPropertyChanged("CheckerBoardIC2");
            }
        }

        private int _CheckerBoardIC3 = 0;
        public int CheckerBoardIC3
        {
            get { return _CheckerBoardIC3; }
            set
            {
                _CheckerBoardIC3 = value;
                OnPropertyChanged("CheckerBoardIC3");
            }
        }

        private int _CheckerBoardIC4 = 0;
        public int CheckerBoardIC4
        {
            get { return _CheckerBoardIC4; }
            set
            {
                _CheckerBoardIC4 = value;
                OnPropertyChanged("CheckerBoardIC4");
            }
        }

        private int _CheckerBoardIC5 = 0;
        public int CheckerBoardIC5
        {
            get { return _CheckerBoardIC5; }
            set
            {
                _CheckerBoardIC5 = value;
                OnPropertyChanged("CheckerBoardIC5");
            }
        }

        private int _CheckerBoardIC6 = 0;
        public int CheckerBoardIC6
        {
            get { return _CheckerBoardIC6; }
            set
            {
                _CheckerBoardIC6 = value;
                OnPropertyChanged("CheckerBoardIC6");
            }
        }

        private int _CheckerBoardIC7 = 0;
        public int CheckerBoardIC7
        {
            get { return _CheckerBoardIC7; }
            set
            {
                _CheckerBoardIC7 = value;
                OnPropertyChanged("CheckerBoardIC7");
            }
        }

        private int _CheckerBoardIC8 = 0;
        public int CheckerBoardIC8
        {
            get { return _CheckerBoardIC8; }
            set
            {
                _CheckerBoardIC8 = value;
                OnPropertyChanged("CheckerBoardIC8");
            }
        }

        private int _CheckerBoardIC9 = 0;
        public int CheckerBoardIC9
        {
            get { return _CheckerBoardIC9; }
            set
            {
                _CheckerBoardIC9 = value;
                OnPropertyChanged("CheckerBoardIC9");
            }
        }

        private int _CheckerBoardIC10 = 0;
        public int CheckerBoardIC10
        {
            get { return _CheckerBoardIC10; }
            set
            {
                _CheckerBoardIC10 = value;
                OnPropertyChanged("CheckerBoardIC10");
            }
        }

        private int _CheckerBoardIC11 = 0;
        public int CheckerBoardIC11
        {
            get { return _CheckerBoardIC11; }
            set
            {
                _CheckerBoardIC11 = value;
                OnPropertyChanged("CheckerBoardIC11");
            }
        }

        private int _CheckerBoardIC12 = 0;
        public int CheckerBoardIC12
        {
            get { return _CheckerBoardIC12; }
            set
            {
                _CheckerBoardIC12 = value;
                OnPropertyChanged("CheckerBoardIC12");
            }
        }

        private int _CheckerBoardIC13 = 0;
        public int CheckerBoardIC13
        {
            get { return _CheckerBoardIC13; }
            set
            {
                _CheckerBoardIC13 = value;
                OnPropertyChanged("CheckerBoardIC13");
            }
        }

        private int _CheckerBoardIC14 = 0;
        public int CheckerBoardIC14
        {
            get { return _CheckerBoardIC14; }
            set
            {
                _CheckerBoardIC14 = value;
                OnPropertyChanged("CheckerBoardIC14");
            }
        }

        private int _CheckerBoardIC15 = 0;
        public int CheckerBoardIC15
        {
            get { return _CheckerBoardIC15; }
            set
            {
                _CheckerBoardIC15 = value;
                OnPropertyChanged("CheckerBoardIC15");
            }
        }

        private int _CheckerBoardIC16 = 0;
        public int CheckerBoardIC16
        {
            get { return _CheckerBoardIC16; }
            set
            {
                _CheckerBoardIC16 = value;
                OnPropertyChanged("CheckerBoardIC16");
            }
        }
        #endregion

        #region DIY LED List 用来记录自定义的LED 字符显示
        private List<RootDIYLedModel> _RootDiyLedModelList = new List<RootDIYLedModel>();

        public List<RootDIYLedModel> RootDiyLedModelList
        {
            get { return _RootDiyLedModelList; }
            set
            {
                _RootDiyLedModelList = value;
                OnPropertyChanged(nameof(RootDiyLedModelList));
            }
        }
        #endregion

        #region TestCodeModels
        private List<TestCodeModel> _TestCodeModels;

        public List<TestCodeModel> TestCodeModels { get { return _TestCodeModels; } set { _TestCodeModels = value; OnPropertyChanged(nameof(TestCodeModels)); } }

        #endregion

        #region TestStationModes
        private List<StationModel> _StationNameDic;
        /// <summary>
        /// PortNames
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public List<StationModel> StationNameDic
        {
            get { return _StationNameDic; }
            set
            {
                _StationNameDic = value;
                OnPropertyChanged("StationNameDic");
            }
        }
        #endregion

        //private StationModel _StationModel;
        ///// <summary>
        ///// 站点名
        ///// </summary>
        //public StationModel StationModel
        //{
        //    get { return _StationModel; }
        //    set
        //    {
        //        _StationModel = value;

        //        OnPropertyChanged("StationModel");

        //        Station = StationNameDic.FirstOrDefault(s => s.StationCode == _StationModel.StationCode).Name;
        //    }
        //}

        #region TestCodeCurrentUpperModels
        private List<TestCodeCurrentUpperModel> _TestCodeCurrentUpperModels;

        public List<TestCodeCurrentUpperModel> TestCodeCurrentUpperModels { get { return _TestCodeCurrentUpperModels; } set { _TestCodeCurrentUpperModels = value; OnPropertyChanged(nameof(TestCodeCurrentUpperModels)); } }

        #endregion

        #region PWMCodeModels
        private List<PWMCodeModel> _PwmCodeModels;

        public List<PWMCodeModel> PwmCodeModels { get { return _PwmCodeModels; } set { _PwmCodeModels = value; OnPropertyChanged(nameof(PwmCodeModels)); } }
        #endregion

        #region 测试指令
        
        private int _TestModelCode;

        public int TestModelCode { 

            get
            { 
                return _TestModelCode;
            } 
            set 
            { 
                _TestModelCode = value; 
                ComputeIoutCurrentValue(); 
                OnPropertyChanged(nameof(TestModelCode));
            } 

        }

        //private TestCodeModel _ModelTestCode;

        //public TestCodeModel ModelTestCode
        //{

        //    get
        //    {
        //        return _ModelTestCode;
        //    }
        //    set
        //    {
        //        _ModelTestCode = value;
               
        //        OnPropertyChanged(nameof(ModelTestCode));

        //        //TestModelCode = _ModelTestCode.TestModelCode;
        //    }

        //}

        private int _PwnModeCode ;

        public int PwnModeCode { get { return _PwnModeCode; } set { _PwnModeCode = value; OnPropertyChanged(nameof(PwnModeCode)); } }


        private bool _IsPwmModeSet = true;
        public bool IsPwmModeSet { get { return _IsPwmModeSet; } set { _IsPwmModeSet = value; OnPropertyChanged(nameof(IsPwmModeSet)); } }

       

        private bool _IsVLedSet = true;
        public bool IsVLedSet { get { return _IsVLedSet; } set { _IsVLedSet = value; OnPropertyChanged(nameof(IsVLedSet)); } }

        private double _DvledValue = 6.72;

        public double DvledValue { get { return _DvledValue; } set { _DvledValue = value; OnPropertyChanged(nameof(DvledValue)); } }


        private bool _IsOCPOVPSet = true;
        public bool IsOCPOVPSet { get { return _IsOCPOVPSet; } set { _IsOCPOVPSet = value; OnPropertyChanged(nameof(IsOCPOVPSet)); } }

        private double _DvLedOvpValue = 8;
        public double DvLedOvpValue { get { return _DvLedOvpValue; } set { _DvLedOvpValue = value; OnPropertyChanged(nameof(DvLedOvpValue)); } }

        private double _DvLedOcpValue = 4;
        public double DvLedOcpValue { get { return _DvLedOcpValue; } set { _DvLedOcpValue = value; OnPropertyChanged(nameof(DvLedOcpValue)); } }

        private double _DICOvpValue = 3.4;
        public double DICOvpValue { get { return _DICOvpValue; } set { _DICOvpValue = value; OnPropertyChanged(nameof(DICOvpValue)); } }

        private double _DICOcpValue = 0.2;
        public double DICOcpValue { get { return _DICOcpValue; } set { _DICOcpValue = value; OnPropertyChanged(nameof(DICOcpValue)); } }

        private bool _IsMCSet = true;

        public bool IsMCSet { get { return _IsMCSet; } set { _IsMCSet = value; OnPropertyChanged(nameof(IsMCSet)); } }

        private int _IMCValue = 1;

        public int IMCValue { get { return _IMCValue; } set { _IMCValue = value; ComputeIoutCurrentValue(); OnPropertyChanged(nameof(IMCValue)); } }

        private bool _IsDCSet = true;

        public bool IsDCSet { get { return _IsDCSet; } set { _IsDCSet = value; OnPropertyChanged(nameof(IsDCSet)); } }

        private int _IDCValue = 1;

        public int IDCValue { get { return _IDCValue; } set { _IDCValue = value; ComputeIoutCurrentValue(); OnPropertyChanged(nameof(IDCValue)); } }//ComputeIoutCurrentValue();

        private bool _IsBCSet = true;

        public bool IsBCSet { get { return _IsBCSet; } set { _IsBCSet = value; OnPropertyChanged(nameof(IsBCSet)); } }

        private int _IBCValue = 1;

        public int IBCValue { get { return _IBCValue; } set { _IBCValue = value; ComputeIoutCurrentValue(); OnPropertyChanged(nameof(IBCValue)); } }//ComputeIoutCurrentValue();

        private bool _IsScaleSet = true;

        public bool IsScaleSet { get { return _IsScaleSet; } set { _IsScaleSet = value; OnPropertyChanged(nameof(IsScaleSet)); } }

        private int _IScaleValue = 1;

        public int IScaleValue { get { return _IScaleValue; } set { _IScaleValue = value; ComputeIoutCurrentValue(); OnPropertyChanged(nameof(IScaleValue)); } }

        private bool _IsSRCSet = true;

        public bool IsSRCSet { get { return _IsSRCSet; } set { _IsSRCSet = value; OnPropertyChanged(nameof(IsSRCSet)); } }

        private int _ISrcValue = 1;

        public int ISrcValue { get { return _ISrcValue; } set { _ISrcValue = value; ComputeIoutCurrentValue(); OnPropertyChanged(nameof(ISrcValue)); } }

        private bool _IsLEDNUMSet = true;

        public bool IsLEDNUMSet { get { return _IsLEDNUMSet; } set { _IsLEDNUMSet = value; OnPropertyChanged(nameof(IsLEDNUMSet)); } }

        private int _ILEDNUMValue = 1;

        public int ILEDNUMValue { get { return _ILEDNUMValue; } set { _ILEDNUMValue = value; OnPropertyChanged(nameof(ILEDNUMValue)); } }


        private bool _IsRSETSet = true;

        public bool IsRSETSet { get { return _IsRSETSet; } set { _IsRSETSet = value; OnPropertyChanged(nameof(IsRSETSet)); } }

        private int _IRSETValue = 1;

        public int IRSETValue { get { return _IRSETValue; } set { _IRSETValue = value; ComputeIoutCurrentValue(); OnPropertyChanged(nameof(IRSETValue)); } }


        private bool _IsCLKSet = true;

        public bool IsCLKSet { get { return _IsCLKSet; } set { _IsCLKSet = value; OnPropertyChanged(nameof(IsCLKSet)); } }

        private int _ICLKValue = 1;

        public int ICLKValue { get { return _ICLKValue; } set { _ICLKValue = value; OnPropertyChanged(nameof(ICLKValue)); } }

        private bool _IsILEDSet = true;

        public bool IsILEDSet { get { return _IsILEDSet; } set { _IsILEDSet = value; OnPropertyChanged(nameof(IsILEDSet)); } }

        private int _IILEDValue = 1;

        public int IILEDValue { get { return _IILEDValue; } set { _IILEDValue = value; ComputeIoutCurrentValue(); OnPropertyChanged(nameof(IILEDValue)); } }

        private string _xlsName;
        public string xlsName
        {
            get { return _xlsName; }
            set
            {
                _xlsName = value;
                OnPropertyChanged("xlsName");
            }
        }


        private double _IoutCurrentValue;

        public double IoutCurrentValue
        {
            get { return _IoutCurrentValue; }
            set
            {
                _IoutCurrentValue = value;
                //ComparerDValue();
                OnPropertyChanged(nameof(IoutCurrentValue));
            }
        }

        #region 计算IOut电流

        public void ComputeIoutCurrentValue()
        {
            switch (this.TestCaseType)
            {
                case 1:
                    {
                        IoutCurrentValue = ComputeIoutForAU586A();
                    }
                    break;

                case 2:
                    {
                        IoutCurrentValue = ComputeIoutForBH003_Bh004();
                    }
                    break;

                case 3:
                    {
                        IoutCurrentValue = ComputeIoutForMax25580();
                    }
                    break;
                case 4:
                    {
                        IoutCurrentValue = ComputeIoutForSD058A();
                    }
                    break;
                case 5:
                    {
                        IoutCurrentValue = ComputeIoutForMax25500();
                    }
                    break;
                default:
                    break;
            }

        }
        private double ComputeIoutForAU586A()
        {
            int MC = 0;

            MC = this.IMCValue;

            return Math.Round(60.0 * MC / 255, 3);
        }
        private double ComputeIoutForSD058A()
        {
            int MC = 0;

            MC = this.IMCValue;

            return Math.Round(30.0 * MC / 255, 3);
        }
        private double ComputeIoutForBH003_Bh004()
        {
            double iMax = 0.0;
            int MC = 0;
            int DC = 0;
            int BC = 0;

            BC = this.IBCValue;
            DC = this.IDCValue;
            MC = this.IMCValue;

            switch (MC)
            {
                case 0:
                    {
                        iMax = 3.2;
                    }
                    break;
                case 1:
                    {
                        iMax = 8.0;
                    }
                    break;
                case 2:
                    {
                        iMax = 11.2;
                    }
                    break;
                case 3:
                    {
                        iMax = 15.9;
                    }
                    break;
                case 4:
                    {
                        iMax = 19.1;
                    }
                    break;
                case 5:
                    {
                        iMax = 23.9;
                    }
                    break;
                case 6:
                    {
                        iMax = 27.1;
                    }
                    break;
                case 7:
                    {
                        iMax = 31.9;
                    }
                    break;
                default:
                    break;
            }

            return Math.Round(iMax * (0.262 + 0.738 * DC / 127) * (0.1 + 0.9 * BC / 127), 3);
        }

        private double ComputeIoutForMax25500()
        {
            double iMax = 0.0;
            int Scale = 0;
            int SRC = 0;
            int RSET = 0;

            Scale = this.IScaleValue;
            SRC = this.ISrcValue;
            RSET = this.IRSETValue;

            iMax = 125 * (Scale + 1) / RSET;

            return Math.Round(iMax * (0.38 + 0.02 * SRC), 3);

        }
        private double ComputeIoutForMax25580()//Iout=(2+2*XA+(0.25+XA*0.25)*XB)*XC/65535=0;
        {
            int Iled = 0;
            int Scale = 0;
            
            Scale = this.IScaleValue;
            Iled = this.IILEDValue;

            return Math.Round(2 + 2 * Scale + (0.25 + Scale * 0.25) * Iled);

        }
        #endregion

        #region DIY计算真实值
        private void ComputeDIYLEDReaCurrent()
        {
            switch (this.TestCaseType)
            {
                case 1:
                    {
                        ComputeReaAUXX();
                    }
                    break;
                case 2:
                    {
                        ComputeReaBH00X();
                    }
                    break;
                case 3:
                    {
                        ComputeReaAU606();
                    }
                    break;
                case 4:
                    {
                        ComputeReaSD058A();
                    }
                    break;
                case 5:
                    {

                    }
                    break;
                default:
                    break;
            }
        }
        private void ComputeReaAUXX()
        {
            int MC = 0;

            MC = this.DIYPictureSetMC;

            double Iout = Math.Round(60.0 * MC / 255, 3);

            if (this.RootDiyLedModelList != null)
            {
                foreach (RootDIYLedModel rootDIYLedModel in this.RootDiyLedModelList)
                {
                    foreach (DIYLedModel dIYLedModel in rootDIYLedModel.DiyLedModelList)
                    {
                        //if (dIYLedModel.LightValue == 128)
                        //{
                        //    dIYLedModel.ReaLightCurrentValue = 0;
                        //}
                        //else
                        //{
                            dIYLedModel.ReaLightCurrentValue = Math.Round(Iout * dIYLedModel.LightValue / 4095, 3);
                        //}
                    }
                }
            }

        }
        private void ComputeReaSD058A()
        {
            int MC = 0;

            MC = this.DIYPictureSetMC;

            double Iout = Math.Round(30.0 * MC / 255, 3);

            if (this.RootDiyLedModelList != null)
            {
                foreach (RootDIYLedModel rootDIYLedModel in this.RootDiyLedModelList)
                {
                    foreach (DIYLedModel dIYLedModel in rootDIYLedModel.DiyLedModelList)
                    {
                        //if (dIYLedModel.LightValue == 128)
                        //{
                        //    dIYLedModel.ReaLightCurrentValue = 0;
                        //}
                        //else
                        //{
                        dIYLedModel.ReaLightCurrentValue = Math.Round(Iout * dIYLedModel.LightValue / 4095, 3);
                        //}
                    }
                }
            }

        }
        private void ComputeReaAU606()
        {
            
            int Scale = DIYPictureSetScale;

            int ILed = DIYPictureSetILED;

            double Iout = (2 + 2 * Scale + ((0.25 + Scale * 0.25) * ILed));

            if (this.RootDiyLedModelList != null)
            {
                foreach (RootDIYLedModel rootDIYLedModel in this.RootDiyLedModelList)
                {
                    foreach (DIYLedModel dIYLedModel in rootDIYLedModel.DiyLedModelList)
                    {
                       
                       dIYLedModel.ReaLightCurrentValue = Math.Round(Iout * dIYLedModel.LightValue / 65535, 3);
                        
                    }
                }
            }

        }
        private void ComputeReaBH00X()
        {
            double iMax = 0.0;
            int MC = 0;
            
            int BC = 0;

            int Value = 0;

            BC = this.DIYPictureSetBC;
           
            MC = this.DIYPictureSetMC;

            Value = this.DIYPictureSetValue;

            switch (MC)
            {
                case 0:
                    {
                        iMax = 3.2;
                    }
                    break;
                case 1:
                    {
                        iMax = 8.0;
                    }
                    break;
                case 2:
                    {
                        iMax = 11.2;
                    }
                    break;
                case 3:
                    {
                        iMax = 15.9;
                    }
                    break;
                case 4:
                    {
                        iMax = 19.1;
                    }
                    break;
                case 5:
                    {
                        iMax = 23.9;
                    }
                    break;
                case 6:
                    {
                        iMax = 27.1;
                    }
                    break;
                case 7:
                    {
                        iMax = 31.9;
                    }
                    break;
                default:
                    break;
            }

            if (this.RootDiyLedModelList != null)
            {
                foreach (RootDIYLedModel rootDIYLedModel in this.RootDiyLedModelList)
                {
                    foreach (DIYLedModel dIYLedModel in rootDIYLedModel.DiyLedModelList)
                    {
                        if (dIYLedModel.LightValue == 128)
                        {
                            dIYLedModel.ReaLightCurrentValue = 0;
                        }
                        else
                        {
                            dIYLedModel.ReaLightCurrentValue = Math.Round((iMax * (0.262 + 0.738 * dIYLedModel.LightValue / 127) * (0.1 + 0.9 * BC / 127)) * Value / 65535, 3);
                        }
                    }
                }
            }

        }
        #endregion

        #endregion

        #region TestItems

        private ObservableCollection<TestItem> _TestItems;
        /// <summary>
        /// Pass Count
        /// </summary>
        public ObservableCollection<TestItem> TestItems
        {
            get { return _TestItems; }
            set
            {
                _TestItems = value;
                OnPropertyChanged("TestItems");
            }
        }

        #endregion

        #region IC亮度数组
        //private List<ICLightModel> _ICLEDLightValueModelList;

        //public List<ICLightModel> ICLEDLightValueModelList
        //{
        //    get { return _ICLEDLightValueModelList; }
        //    set
        //    {
        //        _ICLEDLightValueModelList = value;
        //        OnPropertyChanged(nameof(ICLEDLightValueModelList));
        //    }
        //}
        #endregion

        #region LEDModelList
        private int _DiyLEDRows;
        public int DiyLEDRows
        {
            get { return _DiyLEDRows; }
            set
            {
                _DiyLEDRows = value;
                OnPropertyChanged(nameof(DiyLEDRows));
            }
        }

        private int _DiyLEDCols;
        public int DiyLEDCols
        {
            get { return _DiyLEDCols; }
            set
            {
                _DiyLEDCols = value;
                OnPropertyChanged(nameof(DiyLEDCols));
            }
        }

        private ObservableCollection<DIYLedModel> _DiyLedModelList;// = new List<DIYLedModel>();

        public ObservableCollection<DIYLedModel> DiyLedModelList
        {
            get { return _DiyLedModelList; }
            set
            {
                _DiyLedModelList = value;
                OnPropertyChanged(nameof(DiyLedModelList));
            }
        }

        #endregion

        #region QiPanLEDModelList
        private List<RootDIYLedModel> _RootQiPanLedModelList = new List<RootDIYLedModel>();

        public List<RootDIYLedModel> RootQiPanLedModelList
        {
            get { return _RootQiPanLedModelList; }
            set
            {
                _RootQiPanLedModelList = value;
                OnPropertyChanged(nameof(RootQiPanLedModelList));
            }
        }

        #endregion


        #region DIYPicture Set
        private int _DIYPictureDelayTimeMs;

        public int DIYPictureDelayTimeMs
        {
            get { return _DIYPictureDelayTimeMs;}
            set
            {
                _DIYPictureDelayTimeMs = value;
                OnPropertyChanged(nameof(DIYPictureDelayTimeMs));
            }
        }

        private bool _IsDIYPictureSetMC = true;

        public bool IsDIYPictureSetMC
        {
            get { return _IsDIYPictureSetMC;}
            set
            {
                _IsDIYPictureSetMC = value;
                OnPropertyChanged(nameof(IsDIYPictureSetMC));
            }
        }

        private int _DIYPictureSetMC;
        public int DIYPictureSetMC
        {
            get { return _DIYPictureSetMC;}
            set
            {
                _DIYPictureSetMC = value;
                ComputeDIYLEDReaCurrent();
                OnPropertyChanged(nameof(DIYPictureSetMC));
            }
        }

        private bool _IsDIYPictureSetBC = false;

        public bool IsDIYPictureSetBC
        {
            get { return _IsDIYPictureSetBC; }
            set
            {
                _IsDIYPictureSetBC = value;
                OnPropertyChanged(nameof(IsDIYPictureSetBC));
            }
        }

        private int _DIYPictureSetBC;
        public int DIYPictureSetBC
        {
            get { return _DIYPictureSetBC; }
            set
            {
                _DIYPictureSetBC = value;
                ComputeDIYLEDReaCurrent();
                OnPropertyChanged(nameof(DIYPictureSetBC));
            }
        }

        private bool _IsDIYPictureSetScale = false;

        public bool IsDIYPictureSetScale
        {
            get { return _IsDIYPictureSetScale; }
            set
            {
                _IsDIYPictureSetScale = value;
                OnPropertyChanged(nameof(IsDIYPictureSetScale));
            }
        }

        private int _DIYPictureSetScale;
        public int DIYPictureSetScale
        {
            get { return _DIYPictureSetScale; }
            set
            {
                _DIYPictureSetScale = value;
                ComputeDIYLEDReaCurrent();
                OnPropertyChanged(nameof(DIYPictureSetScale));
            }
        }

        private bool _IsDIYPictureSetILED = false;

        public bool IsDIYPictureSetILED
        {
            get { return _IsDIYPictureSetILED; }
            set
            {
                _IsDIYPictureSetILED = value;
                OnPropertyChanged(nameof(IsDIYPictureSetILED));
            }
        }

        private int _DIYPictureSetILED;
        public int DIYPictureSetILED
        {
            get { return _DIYPictureSetILED; }
            set
            {
                _DIYPictureSetILED = value;

                ComputeDIYLEDReaCurrent();

                OnPropertyChanged(nameof(DIYPictureSetILED));
            }
        }

        private bool _IsDIYPictureSetValue = true;

        public bool IsDIYPictureSetValue
        {
            get { return _IsDIYPictureSetValue; }
            set
            {
                _IsDIYPictureSetValue = value;
                OnPropertyChanged(nameof(IsDIYPictureSetValue));
            }
        }

        private int _DIYPictureSetValue;
        public int DIYPictureSetValue
        {
            get { return _DIYPictureSetValue; }
            set
            {
                _DIYPictureSetValue = value;

                ComputeDIYLEDReaCurrent();

                OnPropertyChanged(nameof(DIYPictureSetValue));
            }
        }

        #endregion


        private int _ICCount;
        public int ICCount
        {
            get { return _ICCount; }
            set
            {
                _ICCount = value;
                OnPropertyChanged(nameof(ICCount));
            }
        }

        private int _IC_PinCount;
        public int IC_PinCount
        {
            get { return _IC_PinCount; }
            set
            {
                _IC_PinCount = value;
                OnPropertyChanged(nameof(IC_PinCount));
            }
        }

        private int _LEDCount;
        public int LEDCount
        {
            get { return _LEDCount; }
            set
            {
                _LEDCount = value;
               
                OnPropertyChanged(nameof(LEDCount));
            }
        }

        private double _CurrentUpper;
        public double CurrentUpper
        {
            get { return _CurrentUpper; }
            set
            {
                _CurrentUpper = value;

                OnPropertyChanged(nameof(CurrentUpper));
            }
        }

        


        public TestConfig Clone()
        {
            TestConfig model = new TestConfig();

            this.CopyTo(model);

            return model;
        }

        public void CopyTo(TestConfig model)
        {
            var ps = this.GetType().GetProperties();

            foreach (var p in ps)
            {
                if (p.CanWrite)
                {
                    p.SetValue(model, p.GetValue(this, null), null);
                    
                }
            }
        }

        #region Config
        private string _Config;
        /// <summary>
        /// 配置工单
        /// </summary>
        [System.Xml.Serialization.XmlIgnore]
        public string Config
        {
            get { return _Config; }
            set
            {
                _Config = value;
                OnPropertyChanged("Config");
                OnPropertyChanged("ConfigName");
            }
        }
        #endregion

        public string ConfigName
        {
            get
            {
                //所以直接返回专案名
                return string.Format("{0}", this.Project);

            }
        }
        public void LoadStationNames()
        {
            string StationNamesStr = ConfigHelper.GetVauleToString("StationNames");

            string[] StationNamesArr = StationNamesStr.Split(';');

            for (int i = 0; i < StationNamesArr.Length; i++)
            {
                string[] CodeArr = StationNamesArr[i].Split(":");

                this.StationNameDic.Add(new StationModel(CodeArr[1], CodeArr[1]));
            }
        }


        public TestConfig()
        {
            this.StationNameDic = new List<StationModel>();
            //this.StationModel = new StationModel("NTC#1","5");
            LoadStationNames();
                
            
        }

        public void InitTestConfig()
        {
            this._DIYPictureDelayTimeMs = 3000;

            //this._ICLEDLightValueModelList = new List<ICLightModel>();

            //for (int i = 0; i < 30; i++) {

            //    this._ICLEDLightValueModelList.Add(new ICLightModel(i.ToString(), 3000, 10));

            //}

            //this._TestCommand = new TestCommand();
            //this._TestCommand.InitTestCommand();
            this._Station = "NTC#1";

            this.TestModelCode = 1;

            //this.StationModel = new StationModel("NTC#1", "5");
            //this.ModelTestCode = new TestCodeModel("AU586A",1);

            this._IsVLedSet = true;
            this._DvledValue = 6.72;
            this._IsOCPOVPSet = true;
            this._DvLedOvpValue = 7;
            this._DvLedOcpValue = 3;
            this._DICOvpValue = 3.5;
            this._DICOcpValue = 0.05;
            this._IsMCSet = true;
            this._IMCValue = 1;
            this._IsDCSet = false;
            this._IDCValue = 1;
            this._IsBCSet = false;
            this._IBCValue = 1;
            this._IsPwmModeSet = false;
            this._PwnModeCode = 0;
            this.xlsName = "LedInfo_AU586A.xlsx";
            this._IsScaleSet = false;
            this._IScaleValue = 1;
            this._IsSRCSet = false;
            this._ISrcValue = 1;
            this._IsLEDNUMSet = false;
            this._ILEDNUMValue = 1;
            this._IsRSETSet = false;
            this._IRSETValue = 1;
            this._IsCLKSet = false;
            this._ICLKValue = 1;
            this._IsILEDSet= false;
            this._IILEDValue= 1;
            this._ICCount = 11;
            this._IC_PinCount = 16;
            this._LEDCount = 176;
            this._CurrentUpper = 12.5;
            this._TestCaseType = 1;

            this._TestCodeModels = new List<TestCodeModel>();
            string CodeModelStr = ConfigHelper.GetVauleToString("TestCodeModels");
            
            string[] CodeModelArr = CodeModelStr.Split(';');

            for (int i = 0;i < CodeModelArr.Length;i++)
            {
                string[] CodeArr = CodeModelArr[i].Split(":");

                this._TestCodeModels.Add(new TestCodeModel(CodeArr[1], int.Parse(CodeArr[0])));
            }

            

            this._TestCodeCurrentUpperModels = new List<TestCodeCurrentUpperModel>();

            string CurrentUpperStr = ConfigHelper.GetVauleToString("CurrentUpper");

            string[] CurrentUpperArr = CurrentUpperStr.Split(';');

            for (int i = 0; i < CurrentUpperArr.Length; i++)
            {
                string[] CodeArr = CurrentUpperArr[i].Split(":");

                this._TestCodeCurrentUpperModels.Add(new TestCodeCurrentUpperModel(double.Parse(CodeArr[1]), int.Parse(CodeArr[0])));
            }


            this._PwmCodeModels = new List<PWMCodeModel>();
            string PWMModelStr = ConfigHelper.GetVauleToString("PWMModes");

            string[] PWMCodeModelArr = PWMModelStr.Split(';');

            for (int i = 0; i < PWMCodeModelArr.Length; i++)
            {
                string[] CodeArr = PWMCodeModelArr[i].Split(":");

                this._PwmCodeModels.Add(new PWMCodeModel(CodeArr[1], int.Parse(CodeArr[0])));
            }

           
            this._TestItems = new ObservableCollection<TestItem>();
            //int num, string testItemCode, string name, bool isTestVisibility, bool isSetMC, int setMCValue, bool isSetDC, int setDCValue, bool isSetBC, int setBCValue,bool isSetScale,int setScaleValue,bool isSetIled,int setILEDValue, bool isSetCurrentValue, double setCurrentValue,  int delayTime, bool isLimit, Limit limit, string unit,bool isReaValue

            this._TestItems.Add(new TestItem(1, "PINShortOpenTest",                     "PIN短路开路测试",         false,false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    false, new Limit(0, 0),          ""    ,false,false,0));
            this._TestItems.Add(new TestItem(2, "DiodePolarityTest",                    "NTC极性测试",             true, false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    false, new Limit(0, 0),          ""    ,false, false, 0));
            this._TestItems.Add(new TestItem(3, "ThermistorTestingRoomTemperature",     "常温下热敏电阻测试(Ω)",  true, false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    true,  new Limit(8000, 12000),   " Ω" ,false, false, 0));
            this._TestItems.Add(new TestItem(4, "ChipStandbyCurrent",                   "芯片待机电流(mA)",        true,false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    true,  new Limit(0, 300),        "mA"  ,false, false, 0));
            this._TestItems.Add(new TestItem(5, "IFBDataTest",                          "IFB电流值测试(uA)",       true,false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    true,  new Limit(0, 300),        "uA"  ,false, false, 0));
            this._TestItems.Add(new TestItem(6, "ChipWorkingCurrent",                   "芯片正常运行电流(mA)",    true,false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    true,  new Limit(10, 300),       "mA"  ,false, false, 0));
            this._TestItems.Add(new TestItem(7, "CurrentTest10uA",                      "Open/Short测试",          true, false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    false, new Limit(0, 0.7),        ""    , false, false, 0));
            this._TestItems.Add(new TestItem(8, "VLEDFullyClosedCurrent",               "VLED亮度全关的漏电流(mA)",true,false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    true,  new Limit(0, 0.7),        "mA"  ,false, false, 0));
            this._TestItems.Add(new TestItem(9, "SingleLEDLowCurrent",                  "单路LED低电流(mA)",       true, true,  0, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 10.7),       "mA"  ,true, false, 0));
            this._TestItems.Add(new TestItem(10, "SingleLEDHighCurrent",                "单路LED高电流(mA)",       true, true,  0, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 13.7),       "mA"  ,true, false, 0));
            this._TestItems.Add(new TestItem(11, "LEDSingleICLowCurrent",               "LED单芯片回路低电流(mA)", true, true,  0, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 4.7),        "mA"  ,true, false, 0));
            this._TestItems.Add(new TestItem(12, "LEDSingleICHighCurrent",              "LED单芯片回路高电流(mA)", true, true,  0, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 20.7),       "mA"  ,true, false, 0));
            this._TestItems.Add(new TestItem(13,"FullScreenLowCurrent",                 "全画面低电流(mA)",        true, true,  0, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 50.7),       "mA"  ,true, false, 0));
            this._TestItems.Add(new TestItem(14,"FullScreenHighCurrent",                "全画面高电流(mA)",        true, true,  0, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 50.7),       "mA"  ,true, false, 0));
            this._TestItems.Add(new TestItem(15,"ThermistorTestingLightingTemperature", "点灯后热敏电阻测试(Ω)",  true, false, 0, false, 0, false, 0,false, 0, false, 0, false, 0,     1,    true,  new Limit(8000, 1200000), " Ω" ,false, false, 0));
            this._TestItems.Add(new TestItem(16,"QiPanPattenDisplay",                   "棋盘格图案显示",          true, false, 0, false, 0, false, 0,false, 0, false, 0, false, 0.1,   1000, false, new Limit(0, 50),         ""    ,false, false, 0));            
            this._TestItems.Add(new TestItem(17,"DIYPattenDisplay",                     "DIY图案显示",             true, false, 0, false, 0, false, 0,false, 0, false, 0, false, 0.1,   1000, false, new Limit(0, 50),         ""    ,false, false, 0));                
            this._TestItems.Add(new TestItem(18,"FullCurrentTest1",                     "全电流测试1(mA)",         true, true,  2, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 100.7),      "mA"  ,true, false, 0));                 
            this._TestItems.Add(new TestItem(19,"FullCurrentTest2",                     "全电流测试2(mA)",         true, true,  3, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 100.7),      "mA"  ,true, false, 0));                 
            this._TestItems.Add(new TestItem(20,"FullCurrentTest3",                     "全电流测试3(mA)",         true, true,  4, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 100.7),      "mA"  ,true, false, 0));                
            this._TestItems.Add(new TestItem(21,"FullCurrentTest4",                     "全电流测试4(mA)",         true, true,  5, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 150.7),      "mA"  ,true, false, 0));                
            this._TestItems.Add(new TestItem(22,"FullCurrentTest5",                     "全电流测试5(mA)",         true, true,  6, false, 0, false, 0,false, 0, false, 0, true , 4095,  1,    true,  new Limit(0, 150.7),      "mA"  ,true, false, 0));               
            this._TestItems.Add(new TestItem(23,"FullCurrentTest6",                     "全电流测试6(mA)",         true, true,  7, false, 0, false, 0, false, 0, false,0, true , 4095,  1,    true,  new Limit(0, 150.7),      "mA"  ,true, false, 0));
            this._TestItems.Add(new TestItem(24,"ESDTest1",                             "ESD测试(奇数)",           true, false, 7, false, 0, false, 0, false, 0, false,0, false , 1,    1,    true, new Limit(0, 150.7),       "mA", false, false, 0));
            this._TestItems.Add(new TestItem(25,"ESDTest2",                             "ESD测试(偶数)",           true, false, 7, false, 0, false, 0, false, 0, false, 0,false,  2,    1, true, new Limit(0, 150.7),       "mA", false, false, 0));

            foreach (TestItem testItem in this.TestItems)
            {
                TestItemDefaults testItemDefaults = App.TestItemDefaultRoot.TestItemDefaults.Find(s => s.name == _TestCodeModels.Find(x => x.TestModelCode == this.TestModelCode).Name);

                bool IsTest = testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).IsTest == "1" ? true : false;

                testItem.IsTest = IsTest;

                testItem.SetMcValue = int.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).SetMC);

                testItem.SetDcValue = int.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).SetDC);

                testItem.SetBcValue = int.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).SetBC);

                testItem.SetCurrentValue = double.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).SetCurrent);

                testItem.SetScaleValue = int.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).SetSCALE);

                testItem.SetILedValue = int.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).SetILED);

                testItem.DelayTime = int.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).Delay);

                testItem.Limit.Upper = double.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).Upper);

                testItem.Limit.Lower = double.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).Lower);

                testItem.IsReaValue = testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).IsReaValue == "1" ? true : false;

                testItem.IsSetCompensationValue = testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).IsSetCompensation == "1" ? true : false;

                testItem.SetCompensationValue = double.Parse(testItemDefaults.TestItemConfig.Find(s => s.name == testItem.TestItemCode).SetCompensationValue);

            }
        }

       
        /// <summary>
        /// Saves the config.
        /// </summary>
        /// <param name="path">Path.</param>
        public void SaveConfig(string path)
        {
            this.FileName = path;

            Bandit.Helpers.ConfigHelper.XmlFileSerialize<TestConfig>(this, path);
        }

        /// <summary>
        /// Saves the config.
        /// </summary>
        public void SaveConfig()
        {
            SaveConfig(this.FileName);
        }

        public static TestConfig LoadConfig(string fileName, int number, bool isLinkFile)
        {
            TestConfig testConfig;

            if (File.Exists(fileName))
            {

                testConfig = Bandit.Helpers.ConfigHelper.XmlFileDeserialize<TestConfig>(fileName);

                //2023年4月6日 17:32:33 应鑫柔Peter要求将Config改成唯一的标识
                testConfig.Config = Bandit.Helpers.FileHelper.GetFileMD5(fileName);

                testConfig.FileName = isLinkFile ? fileName : string.Empty;
            }
            else
            {
                throw new FileNotFoundException(string.Format("[Err] Station{0} Config file not found : {1}", number, fileName));
            }

            Bandit.Helpers.LogHelper.Info(string.Format("# [LoadConfig] Station{0} : {1} #", number, fileName));

            if (isLinkFile && number != -1)
            {
                testConfig.SaveConfig();
            }

            return testConfig;
        }


        public override string ToString()
        {
            return string.Format("{0}-{1}", this.Project, this.Station);
        }
    }
}
